<template>
  <div class="assessment-entry">
    <div class="page-header">
      <h3>测评入口</h3>
      <p>选择要参与的团队测评项目</p>
    </div>

    <div class="entry-container">
      <div class="search-bar">
        <el-form :model="searchForm" inline>
          <el-form-item label="团队名称">
            <el-input v-model="searchForm.teamName" placeholder="请输入团队名称" clearable />
          </el-form-item>
          <el-form-item label="测评状态">
            <el-select v-model="searchForm.status" placeholder="请选择状态" clearable style="width: 150px">
              <el-option label="进行中" value="进行中" />
              <el-option label="未开始" value="未开始" />
              <el-option label="已结束" value="已结束" />
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button type="primary" @click="handleSearch">
              <el-icon><Search /></el-icon>
              搜索
            </el-button>
            <el-button @click="handleReset">
              <el-icon><Refresh /></el-icon>
              重置
            </el-button>
          </el-form-item>
        </el-form>
      </div>

      <div class="assessment-grid">
        <div
          v-for="assessment in assessmentList"
          :key="assessment.id"
          class="assessment-card"
          @click="handleEnterAssessment(assessment)"
        >
          <div class="card-header">
            <div class="team-info">
              <h4>{{ assessment.teamName }}</h4>
              <div class="team-meta">
                <span class="member-count">{{ assessment.memberCount }}人团队</span>
                <el-tag :type="getStatusTagType(assessment.status)" size="small">
                  {{ assessment.status }}
                </el-tag>
              </div>
            </div>
          </div>

          <div class="card-content">
            <div class="assessment-info">
              <div class="info-item">
                <span class="label">测评类型：</span>
                <span class="value">{{ assessment.type }}</span>
              </div>
              <div class="info-item">
                <span class="label">开始时间：</span>
                <span class="value">{{ assessment.startTime }}</span>
              </div>
              <div class="info-item">
                <span class="label">结束时间：</span>
                <span class="value">{{ assessment.endTime }}</span>
              </div>
              <div class="info-item">
                <span class="label">我的进度：</span>
                <div class="progress-info">
                  <el-progress
                    :percentage="assessment.myProgress"
                    :color="getProgressColor(assessment.myProgress)"
                    :stroke-width="6"
                  />
                  <span class="progress-text">{{ assessment.myProgress }}%</span>
                </div>
              </div>
            </div>
          </div>

          <div class="card-footer">
            <el-button
              :type="assessment.status === '进行中' ? 'primary' : 'info'"
              :disabled="assessment.status === '已结束'"
              size="small"
              class="enter-btn"
            >
              {{ getButtonText(assessment) }}
            </el-button>
          </div>
        </div>
      </div>

      <!-- 空状态 -->
      <div v-if="assessmentList.length === 0" class="empty-state">
        <el-empty description="暂无可参与的测评" />
      </div>

      <!-- 分页 -->
      <div class="pagination-container" v-if="assessmentList.length > 0">
        <el-pagination
          v-model:current-page="pagination.currentPage"
          v-model:page-size="pagination.pageSize"
          :page-sizes="[8, 16, 24, 32]"
          :total="pagination.total"
          layout="total, sizes, prev, pager, next, jumper"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </div>

    <!-- 测评详情对话框 -->
    <el-dialog v-model="detailDialogVisible" title="测评详情" width="600px">
      <div v-if="selectedAssessment" class="assessment-detail">
        <div class="detail-section">
          <h4>基本信息</h4>
          <el-descriptions :column="2" border>
            <el-descriptions-item label="团队名称">{{ selectedAssessment.teamName }}</el-descriptions-item>
            <el-descriptions-item label="测评类型">{{ selectedAssessment.type }}</el-descriptions-item>
            <el-descriptions-item label="团队规模">{{ selectedAssessment.memberCount }}人</el-descriptions-item>
            <el-descriptions-item label="测评状态">
              <el-tag :type="getStatusTagType(selectedAssessment.status)">
                {{ selectedAssessment.status }}
              </el-tag>
            </el-descriptions-item>
            <el-descriptions-item label="开始时间">{{ selectedAssessment.startTime }}</el-descriptions-item>
            <el-descriptions-item label="结束时间">{{ selectedAssessment.endTime }}</el-descriptions-item>
          </el-descriptions>
        </div>

        <div class="detail-section">
          <h4>测评说明</h4>
          <p>{{ selectedAssessment.description || '暂无说明' }}</p>
        </div>

        <div class="detail-section">
          <h4>参与进度</h4>
          <div class="progress-detail">
            <div class="progress-item">
              <span>我的完成度：</span>
              <el-progress
                :percentage="selectedAssessment.myProgress"
                :color="getProgressColor(selectedAssessment.myProgress)"
              />
            </div>
            <div class="progress-item">
              <span>团队整体进度：</span>
              <el-progress
                :percentage="selectedAssessment.teamProgress"
                :color="getProgressColor(selectedAssessment.teamProgress)"
              />
            </div>
          </div>
        </div>
      </div>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="detailDialogVisible = false">关闭</el-button>
          <el-button
            type="primary"
            :disabled="selectedAssessment?.status === '已结束'"
            @click="handleStartAssessment"
          >
            {{ selectedAssessment?.myProgress > 0 ? '继续测评' : '开始测评' }}
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { Search, Refresh } from '@element-plus/icons-vue'

// 响应式数据
const detailDialogVisible = ref(false)
const selectedAssessment = ref(null)

// 搜索表单
const searchForm = reactive({
  teamName: '',
  status: ''
})

// 分页数据
const pagination = reactive({
  currentPage: 1,
  pageSize: 8,
  total: 0
})

// 测评数据
const assessmentList = ref([
  {
    id: 1,
    teamName: '产品研发团队',
    memberCount: 8,
    type: '团队角色评估',
    status: '进行中',
    startTime: '2024-08-10 09:00:00',
    endTime: '2024-08-20 18:00:00',
    myProgress: 75,
    teamProgress: 60,
    description: '针对产品研发团队进行的综合角色评估，包括技术能力、协作能力、创新能力等多个维度。'
  },
  {
    id: 2,
    teamName: '市场营销团队',
    memberCount: 6,
    type: '领导力评估',
    status: '未开始',
    startTime: '2024-08-15 09:00:00',
    endTime: '2024-08-25 18:00:00',
    myProgress: 0,
    teamProgress: 0,
    description: '评估团队成员的领导力潜质和市场营销能力。'
  },
  {
    id: 3,
    teamName: '技术支持团队',
    memberCount: 12,
    type: '团队协作评估',
    status: '进行中',
    startTime: '2024-08-08 09:00:00',
    endTime: '2024-08-18 18:00:00',
    myProgress: 100,
    teamProgress: 85,
    description: '评估技术支持团队的协作效率和问题解决能力。'
  },
  {
    id: 4,
    teamName: '人力资源团队',
    memberCount: 5,
    type: '综合能力评估',
    status: '已结束',
    startTime: '2024-07-20 09:00:00',
    endTime: '2024-07-30 18:00:00',
    myProgress: 100,
    teamProgress: 100,
    description: '对人力资源团队进行综合能力评估。'
  }
])

// 方法
const getStatusTagType = (status) => {
  const typeMap = {
    '进行中': 'success',
    '未开始': 'warning',
    '已结束': 'info'
  }
  return typeMap[status] || 'info'
}

const getProgressColor = (percentage) => {
  if (percentage === 100) return '#67c23a'
  if (percentage >= 80) return '#409eff'
  if (percentage >= 60) return '#e6a23c'
  if (percentage >= 40) return '#f56c6c'
  return '#909399'
}

const getButtonText = (assessment) => {
  if (assessment.status === '已结束') return '已结束'
  if (assessment.status === '未开始') return '查看详情'
  if (assessment.myProgress === 0) return '开始测评'
  if (assessment.myProgress === 100) return '查看结果'
  return '继续测评'
}

const handleSearch = () => {
  // 模拟搜索
  ElMessage.success('搜索完成')
}

const handleReset = () => {
  searchForm.teamName = ''
  searchForm.status = ''
  handleSearch()
}

const handleEnterAssessment = (assessment) => {
  selectedAssessment.value = assessment
  detailDialogVisible.value = true
}

const handleStartAssessment = () => {
  if (!selectedAssessment.value) return

  if (selectedAssessment.value.status === '未开始') {
    ElMessage.warning('测评尚未开始')
    return
  }

  if (selectedAssessment.value.status === '已结束') {
    ElMessage.info('测评已结束')
    return
  }

  // 这里跳转到实际的测评页面
  ElMessage.success('正在进入测评...')
  detailDialogVisible.value = false

  // 模拟跳转到测评页面
  setTimeout(() => {
    ElMessage.info('测评权限不够...')
  }, 500)
}

const handleSizeChange = (val) => {
  pagination.pageSize = val
  loadAssessmentData()
}

const handleCurrentChange = (val) => {
  pagination.currentPage = val
  loadAssessmentData()
}

const loadAssessmentData = () => {
  // 模拟加载数据
  pagination.total = assessmentList.value.length
}

onMounted(() => {
  loadAssessmentData()
})
</script>

<style scoped>
.assessment-entry {
  background: white;
  border-radius: 8px;
  padding: 24px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
}

.page-header {
  margin-bottom: 24px;
  padding-bottom: 16px;
  border-bottom: 1px solid #e5e7eb;
}

.page-header h3 {
  margin: 0 0 8px 0;
  color: #1f2937;
  font-size: 20px;
  font-weight: 600;
}

.page-header p {
  margin: 0;
  color: #6b7280;
  font-size: 14px;
}

.search-bar {
  background: #f8fafc;
  padding: 20px;
  border-radius: 8px;
  margin-bottom: 24px;
}

.assessment-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(350px, 1fr));
  gap: 20px;
  margin-bottom: 24px;
}

.assessment-card {
  border: 1px solid #e5e7eb;
  border-radius: 12px;
  padding: 20px;
  background: white;
  cursor: pointer;
  transition: all 0.3s;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.05);
}

.assessment-card:hover {
  box-shadow: 0 8px 25px rgba(0, 0, 0, 0.1);
  transform: translateY(-2px);
  border-color: #5a67d8;
}

.card-header {
  margin-bottom: 16px;
}

.team-info h4 {
  margin: 0 0 8px 0;
  color: #1f2937;
  font-size: 18px;
  font-weight: 600;
}

.team-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.member-count {
  color: #6b7280;
  font-size: 14px;
}

.card-content {
  margin-bottom: 16px;
}

.assessment-info {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.info-item {
  display: flex;
  align-items: center;
  font-size: 14px;
}

.info-item .label {
  color: #6b7280;
  width: 80px;
  flex-shrink: 0;
}

.info-item .value {
  color: #374151;
  flex: 1;
}

.progress-info {
  display: flex;
  align-items: center;
  gap: 8px;
  flex: 1;
}

.progress-info .el-progress {
  flex: 1;
}

.progress-text {
  color: #374151;
  font-size: 12px;
  min-width: 35px;
  text-align: right;
}

.card-footer {
  border-top: 1px solid #f3f4f6;
  padding-top: 16px;
  display: flex;
  justify-content: flex-end;
}

.enter-btn {
  min-width: 80px;
}

.empty-state {
  padding: 60px 0;
  text-align: center;
}

.pagination-container {
  display: flex;
  justify-content: center;
  padding-top: 24px;
  border-top: 1px solid #e5e7eb;
}

.assessment-detail {
  max-height: 500px;
  overflow-y: auto;
}

.detail-section {
  margin-bottom: 24px;
}

.detail-section h4 {
  margin: 0 0 12px 0;
  color: #1f2937;
  font-size: 16px;
  font-weight: 600;
}

.detail-section p {
  margin: 0;
  color: #6b7280;
  line-height: 1.6;
}

.progress-detail {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.progress-item {
  display: flex;
  align-items: center;
  gap: 12px;
}

.progress-item span {
  color: #374151;
  font-size: 14px;
  min-width: 100px;
}

.progress-item .el-progress {
  flex: 1;
}

.dialog-footer {
  display: flex;
  justify-content: flex-end;
  gap: 12px;
}

:deep(.el-progress-bar__outer) {
  border-radius: 10px;
}

:deep(.el-progress-bar__inner) {
  border-radius: 10px;
}
</style>
